<template>
  <div class="home">
    <header class="header">
      <h1>中学生智慧教育平台</h1>
      <div class="user-center">

        <span v-if="user"  >{{ user.email  }}</span> <!-- 显示邮箱 -->
        <button v-if="user" @click="logout">登出</button> <!-- 登出按钮 -->
        <button @click="UserCount">用户中心</button>
      </div>
    </header>
    <main class="content">
      <section class="card">
        <h2>推荐</h2>

      </section>
      <section class="card">
        <h2>推荐</h2>

      </section>
      <section class="card">
        <h2>推荐</h2>

      </section>
      <section class="card">
        <h2>推荐</h2>

      </section>
      <section class="card">
        <h2>推荐</h2>

      </section>
      <section class="card">
        <h2>推荐</h2>

      </section>
      <section class="card">
        <h2>推荐</h2>

      </section>
      <section class="card">
        <h2>推荐</h2>

      </section>


      <section class="card">
        <h2>视频分类</h2>
      </section>
    </main>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: null // 初始化用户信息为 null
    };
  },
  created() {
    this.loadUser(); // 在组件创建时加载用户信息
  },
  methods: {
    loadUser() {
      const user = localStorage.getItem('user');
      if (user) {
        try {
          this.user = JSON.parse(user); // 尝试解析 JSON，确保不会因无效数据引发错误
          console.log("已加载用户信息:", this.user);
          const he=this.user.email;
        } catch (error) {
          console.error("解析用户信息出错:", error);
          localStorage.removeItem('user'); // 如果解析失败，清除无效数据
        }
      }
    },
    logout() {
      localStorage.removeItem('user'); // 清除用户信息
      this.user = null; // 重置用户状态
      this.$router.push('/login'); // 跳转到登录页面
    },
    UserCount(){
      this.$router.push('/UserCount'); // 跳转到用户中心页面
    }
  }
};
</script>

<style scoped>
.home {
  padding: 0px;
  font-family: Arial, sans-serif;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #346af3;
  color: white;
  padding: 7px;
}

.content {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  margin-top: 1px;
}

.card {
  background-color: #f9f9f9;
  border: 1px solid #ccffcc;
  border-radius: 8px;
  padding: 10px;
  margin: 5px;
  height: 80px;
  width: 150px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.card h2 {
  margin-top: 0;
}
</style>
